﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;
using System.Data.Common;

using Data_Objects;
using Business_Objects;
namespace Data_Objects.Implementation
{
    //Ko public -> tất cả gọi qua dataacess
    class SqlServerBaoAnDao : IBaoAnDao
    {
       
        public SqlServerBaoAnDao()
        {

            SQLUtility.TableName = "BaoAn";
        }
        //Trả về đối tượng báo ăn
        private BaoAn MakeBaoAn(DataRow row)
        {
            Guid BaoAnID =Guid.Parse( row["BaoAnID"].ToString());
            DateTime NgayBaoAn =Convert.ToDateTime( row["NgayBaoAn"].ToString());
            Guid ClassID = Guid.Parse(row["ClassID"].ToString());
            Guid StudentID = Guid.Parse(row["StudentID"].ToString());
            Guid SchoolYearID =Guid.Parse( row["SchoolYearID"].ToString());

            bool Baoan =Convert.ToBoolean( row["Baoan"].ToString());
            return new BaoAn(BaoAnID,NgayBaoAn,ClassID,StudentID,SchoolYearID,Baoan);
            //   return new BaoAn(customerId, companyName, city, country);
        }

        //Lấy danh sách khách hàng trả về datatable
        private IList<BaoAn> MakeBaoAns(DataTable dt)
        {
            IList<BaoAn> list = new List<BaoAn>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(MakeBaoAn(row));
            }
            return list;
        }
        public IList<Business_Objects.BaoAn> GetBaoAn()
        {
            SQLUtility.TableName = "BaoAn";
            DbParameter[] param = null;
            DataTable dt = CommandProvider.GetDatatable(SQLUtility.ProceduresGetAll, param);
            return MakeBaoAns(dt);
        }

        public Business_Objects.BaoAn GetBaoAnByID(string BaoAnID)
        {
            SQLUtility.TableName = "BaoAn";
            DbParameter[] parameters = new DbParameter[1];
            parameters[0] = new SqlParameter("@BaoAnID", BaoAnID);
            DataRow row = CommandProvider.GetDataRow(SQLUtility.ProceduresGetByID, parameters);
            if (row == null) { return null; }
            else
            {
                return MakeBaoAn(row);
            }
        }

        public void InsertBaoAn(Business_Objects.BaoAn BaoAn)
        {
            SQLUtility.TableName = "BaoAn";
            int newID = CommandProvider.Insert(SQLUtility.ProceduresInsert, Take(BaoAn), true);
        }

        public int UpdateBaoAn(Business_Objects.BaoAn BaoAn)
        {
            SQLUtility.TableName = "BaoAn";
            try { return CommandProvider.Update(SQLUtility.ProceduresUpdate, Take(BaoAn)); }
            catch { return -1; }
        }

        public int DeleteBaoAn(Business_Objects.BaoAn BaoAn)
        {
            SQLUtility.TableName = "BaoAn";
            try { return CommandProvider.Delete(SQLUtility.ProceduresDelete, Take(BaoAn)); }
            catch { return -1; }
        }


        /// <summary>
        /// Creates query parameters list from Customer object
        /// </summary>
        /// <param name="customer">Customer.</param>
        /// <returns>Name value parameter list.</returns>
        //private object[] Take(BaoAn BaoAn)
        //{
        //    return new object[]  
        //    {
        //        "@BaoAnID", BaoAn.Id,
        //        "@NgayBaoAn", BaoAn.NgayBaoAn,
        //        "@ClassID", BaoAn.ClassID,
        //        "@StudentID", BaoAn.SchoolYearID,
        //        "@SchoolYearID", BaoAn.SchoolYearID,
        //        "@Baoan", BaoAn.Baoan
        //    };
        //}

        private DbParameter[] Take(BaoAn BaoAn)
        {
            DbParameter[] parameters = new DbParameter[6];
            parameters[0] = new SqlParameter("@BaoAnID", BaoAn.Id);
            parameters[1] = new SqlParameter("@NgayBaoAn", BaoAn.NgayBaoAn);
            parameters[2] = new SqlParameter("@ClassID", BaoAn.ClassID);
            parameters[3] = new SqlParameter("@StudentID", BaoAn.StudentID);
            parameters[4] = new SqlParameter("@SchoolYearID", BaoAn.SchoolYearID);
            parameters[5] = new SqlParameter("@Baoan", BaoAn.Baoan);

            return parameters;
        }



        public List<BaoAn> GetBaoAnBySchoolIdAndSchoolYear(string SchoolYearID, int schoolId)
        {
            DbParameter[] parameters = new DbParameter[2];
            parameters[0] = new SqlParameter("@SchoolID", schoolId);
            parameters[1] = new SqlParameter("@SchoolYearID", SchoolYearID);
            DataTable dt = CommandProvider.GetDatatable("usp_SelectBaoAnsByAndSchoolYearIDAndSchoolID", parameters);
            return MakeBaoAns(dt).ToList(); 
        }


        public BaoAn GetBaoAnBySchoolIdAndSchoolYearByStuId(string SchoolYearID, int schoolId, string stuId)
        {
            DbParameter[] parameters = new DbParameter[1];
         
            parameters[0] = new SqlParameter("@StudentID", stuId);
            DataRow row = CommandProvider.GetDataRow("usp_SelectBaoAnByAndStudenID", parameters);
            if (row == null) { return null; }
            else
            {
                return MakeBaoAn(row);
            }
        }


        public List<BaoAn> GetBaoAnBySchoolIdAndSchoolYearAndNgayBaoAn(string SchoolYearID, int schoolId, DateTime ngayBaoAn)
        {
            DbParameter[] parameters = new DbParameter[3];
            parameters[0] = new SqlParameter("@SchoolID", schoolId);
            parameters[1] = new SqlParameter("@SchoolYearID", SchoolYearID);
            parameters[2] = new SqlParameter("@NgayBaoAn", ngayBaoAn);
            DataTable dt = CommandProvider.GetDatatable("usp_SelectBaoAnsByAndSchoolYearIDAndSchoolIDAndNgayBaoAn", parameters);
            return MakeBaoAns(dt).ToList(); 
        }
    }
}
